package com.ml.loan.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.loan.common.BaseController;
import com.ml.loan.common.annotaion.OperLog;
import com.ml.loan.common.model.JsonResult;
import com.ml.loan.common.model.PageResult;
import com.ml.loan.common.redis.RedisService;
import com.ml.loan.sys.model.Authorities;
import com.ml.loan.sys.service.AuthoritiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 权限管理
 **/
@Api(tags = "菜单权限模块")
@Controller
@RequestMapping("/system/authorities")
public class AuthoritiesController extends BaseController {
    @Autowired
    private AuthoritiesService authoritiesService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("authorities:view")
    @GetMapping()
    public String authorities(Model model) {
        List<Authorities> authorities = authoritiesService.list(new QueryWrapper<Authorities>().eq("is_menu", 0).orderByAsc("order_number"));
        model.addAttribute("authorities", authorities);
        return "system/authorities.html";
    }

    /**
     * 查询所有权限
     **/
    @RequiresPermissions("authorities:view")
    @ResponseBody
    @GetMapping("/list")
    @OperLog(module = "SYS", desc = "系统权限列表-查询")
    public PageResult<Authorities> list() {
        List<Authorities> authorities = authoritiesService.list(new QueryWrapper<Authorities>().orderByAsc("order_number"));
        return new PageResult<>(authorities);
    }

    /**
     * 添加权限
     */
    @RequiresPermissions("authorities:update")
    @ResponseBody
    @RequestMapping("/add")
    @OperLog(module = "SYS", desc = "系统权限列表-新增", result = true)
    public JsonResult add(Authorities authorities) {
        if (authoritiesService.save(authorities)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改权限
     */
    @RequiresPermissions("authorities:update")
    @ResponseBody
    @RequestMapping("/update")
    @OperLog(module = "SYS", desc = "系统权限列表-更新", result = true)
    public JsonResult update(Authorities authorities) {
        if (authoritiesService.updateById(authorities)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @RequiresPermissions("authorities:update")
    @ResponseBody
    @RequestMapping("/updateState")
    @OperLog(module = "SYS", desc = "系统权限列表-状态更新", result = true)
    public JsonResult updateState(Integer id, Integer state) {
        if (id == null) {
            return JsonResult.error("参数id不能为空");
        }
        if (state == null || (state != 1 && state != 0)) {
            return JsonResult.error("状态值不正确");
        }
        Authorities authorities = new Authorities();
        authorities.setAuthorityId(id);
        authorities.setStatus(state);
        if (authoritiesService.updateById(authorities)) {
            return JsonResult.ok();
        } else {
            return JsonResult.error();
        }
    }


    /**
     * 删除权限
     */
    @RequiresPermissions("authorities:update")
    @ResponseBody
    @RequestMapping("/delete")
    @OperLog(module = "SYS", desc = "系统权限列表-删除", result = true)
    public JsonResult delete(Integer authorityId) {
        if (authoritiesService.removeById(authorityId)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }
}
